/* 
 * Copyright [2018] [Alex/libo(liboms@hotmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.restlet.server;

import java.lang.reflect.Method;
import java.util.logging.Logger;

import javax.ws.rs.Path;

import org.restlet.Component;

import com.hyts.restlet.annotation.REST;
import com.hyts.restlet.application.RestApplication;
import com.hyts.restlet.base.DispatcherApplication;
import com.hyts.restlet.model.ServerModel;


/** 
 * @title BootstrapServer.java  
 * @package com.hyts.trestlet.server  
 * @author libo/Alex
 * @email liboms@hotmail.com/liboware@163.com  
 * @date 2018年5月13日 
 * @version V1.0 
 */
@REST("com.hyts")
public final class BootstrapServer {

	/**  
	 * <p>日志输出器</p> 
	 * @fieldName logger
	 * @fieldType Logger
	 */ 
	private static Logger logger = LoggerManager.getLogger(BootstrapServer.class);

	private String packageName;

    /**  
     * <p>服务组建</p> 
     * @fieldName component
     * @fieldType Component
     */ 
    Component component = new Component();
/*
	{
		REST anno = (REST) this.getClass().getAnnotation(REST.class);
		packageName = (anno.value() == null || "".equals(anno.value())?this.getClass().getPackage().getName();
	}*/

	/**
	 * <b>start:启动服务Rest发布</b>
	 * <p>启动服务Rest发布</p>
	 * @param model : 服务模型对象
	 * @exception
	 */ 
	public void start(String packageName,ServerModel model) throws Exception{
		//启动服务Rest
		synchronized(this){
			logger.info("系统开始扫描注解......");
			ScanBaseObject scanBaseObject = new ScanBaseObject();
			scanBaseObject.annotations = new Class[] {Path.class};
			scanBaseObject.packages = new String[] {packageName};
			new SourceAndJarHandler(true).scan(scanBaseObject)
							.scanResultClass.forEach(param->{
				Path path = param.getAnnotation(Path.class);
				Method[] methods = param.getDeclaredMethods();
				for(Method method:methods){
					Path subpath = method.getAnnotation(Path.class);
					if( subpath != null) {
						logger.info("【coffice-toolbox-restlet】---------------映射类：" + param.getName() + " -> " + "映射路径:" + path.value()+subpath.value()+"---------------【coffice-toolbox-restlet】");
					}
				}
				DispatcherApplication.getRouteList().add(param);
			});
			logger.info("【coffice-toolbox-restlet】---------------服务开始启动......---------------【coffice-toolbox-restlet】");
			component.getServers().add(model.getProtocol(),model.getIp(),model.getPort());
			component.getDefaultHost().attach(new RestApplication(component.getContext()));
			component.start();
			logger.info("【coffice-toolbox-restlet】---------------服务启动完成结束 ---------------【coffice-toolbox-restlet】");
		}
	}
	
	/**  
	 * <b>start:启动服务Rest发布</b>
	 * <p>启动服务Rest发布</p>
	 * @throws Exception
	 * @exception
	 */ 
	public void start(String packageName) throws Exception{
		ServerModel model = new ServerModel();
		start(packageName,model);
	}


	/**
	 * @param args
	 * @exception
	 */ 
	public static void main(String[] args) {
		try {
			new BootstrapServer().start("com.hyts");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
